package cn.com.lyj6851.modules.shop.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.shop.domain.YdySystemConfig;
import cn.com.lyj6851.modules.shop.service.YdySystemConfigService;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemConfigDto;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemConfigQueryCriteria;
import cn.com.lyj6851.modules.shop.service.mapper.SystemConfigMapper;
import cn.com.lyj6851.utils.FileUtil;
import cn.com.lyj6851.utils.RedisUtils;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdySystemConfigServiceImpl extends BaseServiceImpl<SystemConfigMapper, YdySystemConfig> implements YdySystemConfigService {
    private final IGenerator generator;
    private final RedisUtils redisUtils;

    /**
     * 根据名称获取
     * @param name 配置名
     */
    @Override
    public String getData(String name) {
        String result = redisUtils.getY(name);
        if(StrUtil.isNotBlank(result)) {
            return result;
        }
        QueryWrapper<YdySystemConfig> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YdySystemConfig::getMenuName,name);
        YdySystemConfig systemConfig = baseMapper.selectOne(wrapper);
        if(systemConfig == null){
            return "";
        }
        return systemConfig.getValue();
    }

    @Override
    public Map<String, Object> queryAll(YdySystemConfigQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdySystemConfig> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdySystemConfigDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<YdySystemConfig> queryAll(YdySystemConfigQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdySystemConfig.class,criteria));
    }

    @Override
    public void download(List<YdySystemConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdySystemConfigDto ydySystemConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("字段名称", ydySystemConfig.getMenuName());
            map.put("默认值", ydySystemConfig.getValue());
            map.put("排序", ydySystemConfig.getSort());
            map.put("是否隐藏", ydySystemConfig.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 根据key值查询
     */
    @Override
    public YdySystemConfig findByKey(String key) {
        return getOne(new QueryWrapper<YdySystemConfig>().lambda().eq(YdySystemConfig::getMenuName,key));
    }
}
